package com.five.matebook.activity.db;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

public class QueryFilter implements Parcelable {

    private String outputColume;
    private String selection;
    private String[] selectionArgs;
    private String orderBy;
    private String limit;
    private QueryFilter(String outputColume, String selection, String[] selectionArgs, String orderBy, String limit) {
        this.outputColume = outputColume;
        this.selection = selection;
        this.selectionArgs = selectionArgs;
        this.orderBy = orderBy;
        this.limit = limit;
    }

    protected QueryFilter(Parcel in) {
        outputColume = in.readString();
        selection = in.readString();
        selectionArgs = in.createStringArray();
        orderBy = in.readString();
        limit = in.readString();
    }

    public static final Creator<QueryFilter> CREATOR = new Creator<QueryFilter>() {
        @Override
        public QueryFilter createFromParcel(Parcel in) {
            return new QueryFilter(in);
        }

        @Override
        public QueryFilter[] newArray(int size) {
            return new QueryFilter[size];
        }
    };

    public String getLimit() {
        return limit;
    }

    public String getOutputColume() {
        return outputColume;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public String getSelection() {
        return selection;
    }

    public String[] getSelectionArgs() {
        return selectionArgs;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(outputColume);
        dest.writeString(selection);
        dest.writeStringArray(selectionArgs);
        dest.writeString(orderBy);
        dest.writeString(limit);
    }

    public static class Builder {
        private StringJoiner selection = new StringJoiner(" and ");
        private StringJoiner orderBy = new StringJoiner(", ");
        private List<String> selectionArgsList = new ArrayList<>();
        private String outputColume = "*";
        private String limit = null;

        public Builder addLongRange(String colume, long begin, long end) {
            selection.add(String.format("%s >= %s and %s <= %s", colume, begin, colume, end));
            return this;
        }

        public Builder addFloatRange(String colume, float begin, float end) {
            selection.add(String.format("%s >= %s and %s <= %s", colume, begin, colume, end));
            return this;
        }

        public Builder addStringMatch(String colume, String targetStr, boolean fullMatch) {
            if (!fullMatch) {
                selection.add(String.format("%s LIKE '%s%s%s' ", colume, "%", targetStr, "%"));
            } else {
                selection.add(String.format("%s = '%s' ", colume, targetStr));
            }
            return this;
        }

        public Builder setOrderBy(String colume, boolean isDesc) {
            if (isDesc) {
                // 降序
                this.orderBy.add(String.format("%s %s", colume, "DESC"));
            } else {
                // 升序
                this.orderBy.add(String.format("%s %s", colume, "ASC"));
            }
            return this;
        }

        public Builder sumbColume(String colume, String newColume) {
            outputColume = String.format("SUM(%s) as %s", colume, newColume);
            return this;
        }

        public Builder setLimit(int count) {
            limit = String.valueOf(count);
            return this;
        }

        public QueryFilter build() {
            return new QueryFilter(this.outputColume, this.selection.toString(), this.selectionArgsList.toArray(new String[0]), this.orderBy.toString(), this.limit);
        }
    }
}
